
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-peccancyModuleConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#peccancyModuleConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $reviewTypeDiv = $view.find('#reviewTypeDiv'),
        $issuedTypeDiv = $view.find('#issuedTypeDiv'),
        tableIns;

    form.verify(validate);

    initFormValue();

    function initFormValue() {
        if (peccancyModuleConfig) {
            let reviewSwitch = peccancyModuleConfig.reviewSwitch;
            let issuedSwitch = peccancyModuleConfig.issuedSwitch;
            form.val("peccancyModuleConfig-form", {
                "moduleConfigId": peccancyModuleConfig.moduleConfigId,
                "tenantId": peccancyModuleConfig.tenantId,
                "reviewSwitch": reviewSwitch == 1 ? 'on' : '',
                "reviewType": peccancyModuleConfig.reviewType,
                "issuedSwitch": issuedSwitch == 1 ? 'on' : '',
                "issuedType": peccancyModuleConfig.issuedType,
            });
            if (reviewSwitch == 1) {
                $reviewTypeDiv.show();
            }else {
                $reviewTypeDiv.hide();
            }
            if (issuedSwitch == 1) {
                $issuedTypeDiv.show();
            }else {
                $issuedTypeDiv.hide();
            }

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'peccancyModuleConfig/search/' + tenantId, null, function (data) {
            var newPeccancyModuleConfig = "";
            if(data.code==200&&data.data.peccancyModuleConfig){
                newPeccancyModuleConfig = data.data.peccancyModuleConfig;
            }
            let reviewSwitch = newPeccancyModuleConfig.reviewSwitch;
            let issuedSwitch = newPeccancyModuleConfig.issuedSwitch;
            form.val("peccancyModuleConfig-form", {
                "moduleConfigId": newPeccancyModuleConfig.moduleConfigId,
                "tenantId": newPeccancyModuleConfig.tenantId,
                "reviewSwitch": reviewSwitch == 1 ? 'on' : '',
                "reviewType": newPeccancyModuleConfig.reviewType ? newPeccancyModuleConfig.reviewType : '1',
                "issuedSwitch": issuedSwitch == 1 ? 'on' : '',
                "issuedType": newPeccancyModuleConfig.issuedType ? newPeccancyModuleConfig.issuedType : '1',
            });
            if (reviewSwitch == 1) {
                $reviewTypeDiv.show();
            }else {
                $reviewTypeDiv.hide();
            }
            if (issuedSwitch == 1) {
                $issuedTypeDiv.show();
            }else {
                $issuedTypeDiv.hide();
            }

        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    // 监听审核开关
    form.on('switch(reviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $reviewTypeDiv.show();
        }else {
            $reviewTypeDiv.hide();
        }
    })
    // 监听签发开关
    form.on('switch(issuedSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $issuedTypeDiv.show();
        }else {
            $issuedTypeDiv.hide();
        }
    })

    form.on('submit(peccancyModuleConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        data.field.reviewSwitch = data.field.reviewSwitch == 'on' ? 1 : 0
        data.field.issuedSwitch = data.field.issuedSwitch == 'on' ? 1 : 0
        febs.modal.confirm('保存提示', '是否保存配置信息？', function () {
            if (data.field.moduleConfigId && data.field.moduleConfigId.length > 0) {
                febs.postArray(ctx + 'peccancyModuleConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'peccancyModuleConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });


    form.render();


});